<?php

class AccountAction extends Action {
	
	public function _initialize(){
// 		cookie('openid','oyEL0t1BljoYFf2Sbw3ghqVi36RI',86400*100);
		Vendor("WxJS.AutoLoad");
		$jssdk = new JSSDK(C("WX_APP_ID"), C("WX_APP_SECRET"));
		$signPackage = $jssdk->GetSignPackage();
		$this->assign('signPackage',$signPackage);
	}
	//空操作跳转
	public function _empty() {
		$this->redirect('account/consume');
	}
	
	//注册会员
	public function register() {
		$title = "注册会员";
		
		if($_POST && session('code') == ($_POST['verify'])){
			$data1 = array();
			$_POST['openid'] = getOpenID();
			$customer = webservice("Customer","register",$data1, $_POST);
			session('code',null);
			$this->redirect('account/message');
			return ;
		}
		
		$this->display('account/register');
	}
	
	function shareSuccess(){
		$openID = getOpenID();
		$customer = webservice("Customer","shareSuccess",array('openid'=>$openID));
	}
	
	//显示
	public function show() {
		dayin($_SESSION);
	}
	
	//导航用
	public function home() {
	    global $_GLOBAL;
	    
		$post['name'] = "lin";
		$post['mobile'] = "lin";
		$post['sex'] = "lin";
		$customer = webservice("Customer","register",array(), $post);
// 	    cookie('openid','oyEL0t1BljoYFf2Sbw3ghqVi36RI',86400*100); //demo 3c

		echo $_GLOBAL['WebService_getLastUrl'];
		print_r($_GLOBAL['WebService_getLastPost']);
		print_r($customer);
// 		cookie('openid','oyEL0t8AG2GPYvOwIfefR5mshOkc',86400*100);
		$this->display('account/home');
	}
	
	//账户信息
	public function message() {
		global $_GLOBAL;
		
		$title = "账户信息";

// 		print_r($_CO);
// 		cookie('openid','oyEL0t8AG2GPYvOwIfefR5mshOkc',86400*100);
// 		cookie('openid','oyEL0t1BljoYFf2Sbw3ghqVi36RI',86400*100);
// 		$this->display('account/message');
// 		return ;
// 		

		
		
		$openID = getOpenID();
		$data['openid'] = $openID;
		$info = webservice("Customer","InfoByOpenID",$data);
		
		if($info['NoUser']=='1'){
			header('location:/club/account/bind');
			return ;
		}
		
		$data1['id'] = $info['Id'];
		$CardList = webservice("Customer","Card",$data1);
		
		$LcList = webservice("Customer","lc",$data1);
		
		$GiftList = webservice("Customer","gift",$data1);
		
		$NonePayList = webservice("Customer","nonePay",$data1);
		
		$this->assign('title',$title);
		$this->assign('info',$info);
		
		$this->assign('CardList',$CardList);
		$this->assign('LcList',$LcList);
		$this->assign('NonePayList',$NonePayList);
		$this->assign('GiftList',$GiftList);
		
		$this->display('account/message');
	}
	
	//消费明细
	public function consume() {
		$title = "消费记录";
		$this->assign('title',$title);
			
		
		
		$openID = getOpenID();
		$data['openid'] = $openID;
		$info = webservice("Customer","InfoByOpenID",$data);
		$this->assign('info',$info);
		
		
		if($info['NoUser']=='1'){
			header('location:/club/account/bind');
			return ;
		}
		
		$data1['id'] = $info['Id'];
		$starttime = empty($_POST['starttime'])?date('Y-m-d',time()-86400*30):$_POST['starttime'];
		$endtime = empty($_POST['endtime'])?date('Y-m-d'):$_POST['endtime'];
		$data1['starttime'] = $starttime;
		$data1['endtime'] = $endtime;
		$CList = webservice("Customer","consume",$data1);
		
		
		$this->assign('CList',$CList);
		
		$this->assign('starttime',$starttime);
		$this->assign('endtime',$endtime);
		
		$this->display('account/consume');
	}
	
	//在线预约
	public function reserve() {
		$title = "在线预约";
		$this->assign('title',$title);
		
		if($_GET['No']){
			$cclist = webservice("System","ProjectSet",array('No'=>$_GET['No']));
			echo json_encode($cclist);
			return ;
		}
		
		$openID = getOpenID();
// 		$openID = "oyEL0t8AG2GPYvOwIfefR5mshOkc";
		$data['openid'] = $openID;
		$info = webservice("Customer","InfoByOpenID",$data);
		$this->assign('info',$info);
		if($info['NoUser']=='1'){
		    header('location:/club/account/bind');
		    return ;
		}
		
		//疗程项目
		$plist = webservice("Customer","Project",array('id'=>$info['Id']));
		$this->assign('plist',$plist);
		
		//大分类
		$cblist = webservice("System","ProjectKind");
		$this->assign('cblist',$cblist);
		
		$slist = webservice("System","Store");
		$this->assign('slist',$slist);
			
		$this->display('account/reserve_type');
	}
	
	//预约下单
	public function reserve_order() {
		
		$openID = getOpenID();
		
		$data['openid'] = $openID;
		$info = webservice("Customer","InfoByOpenID",$data);
		$this->assign('info',$info);
		if($info['NoUser']=='1'){
		    header('location:/club/account/bind');
		    return ;
		    exit();
		}
		
		
		
		if($_POST['reserve']=='add'){
			$add = $_POST;
			$add['OrderDate'] = $_POST['date'].' '.$_POST['time'];
			$add['wxOpenID'] = $openID;
			$add['IssueDate'] = date('Y-m-d H:i:s');
			$add['Fstate'] = 0;
			$add['IsLook'] = 0;
			unset($add['date']);
			unset($add['time']);
			unset($add['reserve']);
			$info = webservice("Customer","reserve",array(),$add);
			
			$yylist = explode(',', $_POST['ProductPackageNameStr']);
			$this->assign('msg','您已预约成功');
			$this->assign('yylist',$yylist);
			$this->display('account/success');
			return ;
		}
		
		
		
		
		foreach($_POST['ids'] as $key=>$value){
			$tmp[] = "'{$value}'";
		}
		$s = implode(",", $tmp);
		$cblist = webservice("System","ProjectSetByCode",array('ids'=>$s));
		$this->assign('cblist',$cblist);
		
		if(empty($cblist)){
// 		    $this->redirect('/club/account/reserve');
			header('location:/club/account/reserve');
			return ;
		}
// 		print_r($cblist);
		//门店
		$store = webservice("System","Store");
		$this->assign('store',$store);
		
		foreach($cblist as $key=>$value){
			$name[] = $value['ProjectName'];
			$ids[] = $value['ID'];
 		}
		
		$this->assign('ProjectName',implode(',', $name));
		$this->assign('ProjectID',implode(',', $ids));
		$this->display('account/reserve_order');
	}
	
	
	
	
	//美丽诊断
	public function diagnose() {
		$title = "美丽诊断";
		$this->assign('title',$title);
			
		$this->display('account/diagnose');
	}
	
	//文章列表
	public function hot() {
		
		
		$openID = getOpenID();
		$data['openid'] = $openID;
		$info = webservice("Customer","InfoByOpenID",$data);
		
		if($info['NoUser']=='1'){
			header('location:/club/account/bind');
			return ;
		}
		
		//推荐热销项目
		$db = D('Project');
		$map = array();
		$map['status'] = 1;
		$project = $db->where($map)->order('myorder desc,ctime desc')->getlist();
		
		//预定
		if($_POST) {
			if(is_array($_POST['pid'])) {//从列表页提交
				$pid = $_POST['pid'][0];
			}else{//从详情页提交
				$pid = $_POST['pid'];
			}
			
			//查询当前的项目
			$map = array();
			$map['id'] = $pid;
			$map['status'] = 1;
			$cur = $db->where($map)->find();
			
			$db2 = D('Reservation');
			$data = array();
			$data['openid'] = $openID;
			$data['customer_id'] = $info['CustomerNo'];
			$data['project_id'] = $pid;
			$data['project_price'] = $cur['price'];
			$data['ctime'] = date('Y-m-d H:i:s');
			$id = $db2->join($data);
			$this->redirect('shop/pay',array('reservation_id'=>$id));//跳往付款页面
		}
		
		$this->assign('openID',$openID);
		$this->assign('project',$project);	
		$this->display('account/plist');
	}
	
	//文章详情
	public function project() {
		$title = "文章详情";
		$this->assign('title',$title);
		
		$openID = getOpenID();
		$data['openid'] = $openID;
		$info = webservice("Customer","InfoByOpenID",$data);
		
		if($info['NoUser']=='1'){
			header('location:/club/account/bind');
			return ;
		}
		
		//如果本页带有fromOpenID
		if($_GET['fromOpenID']!='') {
			$data = array();
			$data['fromOpenID'] = $_GET['fromOpenID'];
			$data['openid'] = $openid;
			$clist1 = webservice("Customer","wx_RecommendedCustomer",$data);
		}else{
			$this->redirect('account/project',array('pid'=>$_GET['pid'],'fromOpenID'=>$openID));//跳往本页
		}
		
		//推荐热销项目
		$db = D('Project');
		$map = array();
		$map['id'] = $_GET['pid'];
		$map['status'] = 1;
		$project = $db->where($map)->get();
		if(empty($project)) {
			$this->redirect('account/hot');
		}
		
		$this->assign('openID',$openID);
		$this->assign('project',$project);
		$this->display('account/project');
	}
	
	//订单列表
	public function reservation() {
		$title = "购物查询";
		$this->assign('title',$title);
		
		$openID = getOpenID();
		$data['openid'] = $openID;
		$info = webservice("Customer","InfoByOpenID",$data);
		
		if($info['NoUser']=='1'){
			header('location:/club/account/bind');
			return ;
		}
		
		$db = D('Reservation');
		$map = array();
		$map['customer_id'] = $info['CustomerNo'];
		$result = $db->where($map)->order('ctime desc')->getlist();
		
		$this->assign('result',$result);
		$this->display('account/reservation');
	}
	
	//预约列表
	public function book() {
		$title = "预约列表";
		$this->assign('title',$title);
		
		$openID = getOpenID();
		$data['openid'] = $openID;
		$clist = webservice("Customer","BookList",$data);
		$clist = object_array($clist);
			
		$this->assign('clist',$clist);
		$this->display('account/book');
	}
	
	
	//客户喜爱
	public function love() {
		$title = "客户喜爱";
		$this->assign('title',$title);
			
		$this->display('account/plist');
	}
	
	//美丽商城
	public function shop() {
		$title = "美丽商城";
		$this->assign('title',$title);
			
		$this->display('account/shop');
	}
	
	//客服中心
	public function server() {
		$title = "美丽商城";
		$this->assign('title',$title);
			
		$this->display('account/server');
	}
	
	//绑定发送短信
	function bindsms(){
		if(session('verify') != md5($_POST['imgverify'])){
			$s['status'] = 0;
			$s['msg'] = "请输入正确的图形验证码";
			echo json_encode($s);
			return ;
		}
		
		$data['mobile'] = $_POST['mobile'];
		$info = webservice("Customer","InfoByMobile",$data);
		if(empty($info)){
			$s['status'] = 0;
			$s['msg'] = "该手机号码不是会员";
			echo json_encode($s);
			return ;
		}
		
		$data['mobile'] = $_POST['mobile'];
		$data['code'] = rand(1000,9999);
		session('code',$data['code']);
		webservice("Sms","smsverify",$data);
		
		$s['status'] = 1;
		$s['msg'] = "发送成功";
		echo json_encode($s);
		return ;
		
	}
	
	function bindsmsnovip(){
		if(session('verify') != md5($_POST['imgverify'])){
			$s['status'] = 0;
			$s['msg'] = "请输入正确的图形验证码";
			echo json_encode($s);
			return ;
		}
	
		$data['mobile'] = $_POST['mobile'];
		$data['code'] = rand(1000,9999);
		session('code',$data['code']);
		webservice("Sms","smsverify",$data);
	
		$s['status'] = 1;
		$s['msg'] = "发送成功";
		echo json_encode($s);
		return ;
	
	}
	//绑定会员
	public function bind() {
		$title = "绑定会员";
		
		$openID = getOpenID();
		
		if($_POST && session('code') == ($_POST['verify']) && $_POST['verify']!=''){
			$data['openid'] = $openID;
			$data['mobile'] = $_POST['mobile'];
			$info = webservice("Customer","bind",$data);
			
			$this->assign('url','/club/account/message');
			$this->success();
			return ;
		}
		
		$this->assign('openID',$openID);
		$this->assign('title',$title);
		$this->display('account/bind');
	}
	
	function success(){
		$this->display('account/success');
	}
	
	//消费评价
	function comment(){
		$title = "消费评价";
		
		if($_POST){
			$data = array();
			$openID = getOpenID();
			$post['OpenID'] = $openID;
			$post['SrcID'] = $_GET['id'];
			$post['ProjectGrade'] = $_POST['ProjectGrade'];
			$post['ServiceAttitudeGrade'] = $_POST['ServiceAttitudeGrade'];;
			$post['ServiceEmployeeGrade'] = $_POST['ServiceEmployeeGrade'];;
			$post['Evaluation'] = $_POST['Evaluation'];;
			$info = webservice("Customer","comment",$data,$post);
			
			$this->assign('msg','您已评价成功，感谢您对我们服务的支持');
			$this->display('account/success');
			return ;
		}
		
		$data['id'] = $_GET['id'];
		$info = webservice("Customer","comment",$data,$post);
		
		if($info){
			$this->assign('msg','您已评价成功，感谢您对我们服务的支持');
			$this->display('account/success');
			return ;
		}
		$this->assign('info',$info);
		$this->assign('title',$title);
		$this->display('account/comment');
	}
	
	function HealthTrace(){
		
		if($_POST){
			foreach($_POST['ids'] as $key=>$value){
				$tmp[] = "'{$value}'";
			}
			$s = implode(",", $tmp);
			
			$data = array('ids'=>$s);
			$data['cid'] = $_POST['cid'];
			$data['name'] = $_POST['name'];
			
			$info = webservice("Products","HealthSave",$data);
			
			
			$this->assign('url','/club/account/HealthTraceReport');
			$this->assign('msg','已收到您的提交<br>正在生成报告...');
			$this->success();
			return ;
			
// 			print_r($info);
// 			exit();
		}
		
		$openID = getOpenID();
		$data['openid'] = $openID;
		$info = webservice("Customer","InfoByOpenID",$data);
		$this->assign('info',$info);
		
		if($info['NoUser']=='1'){
			header('location:/club/account/bind');
			return ;
		}
		
		//HealthClass
		$clist = webservice("Products","HealthClass");
		
		$this->assign('clist',$clist);
		$this->display('account/HealthTrace');
	}
	
	function HealthTraceReport(){
		
// 		HealthReport
		
		$openID = getOpenID();
		$data['openid'] = $openID;
		$info = webservice("Customer","InfoByOpenID",$data);
		$this->assign('info',$info);
		
		if($info['NoUser']=='1'){
			header('location:/club/account/bind');
			return ;
		}
		$carray = webservice("Products","HealthReport",array('id'=>$info['Id']));
		
		$clist = array();
		foreach($carray as $key=>$value){
		    //StatusName
		    $clist[$value['StatusName']][] = $value;
		}
		
// 		print_r($clist);
		$this->assign('clist',$clist);
		$this->display('account/HealthReport');
	}
	
	function test(){
		
		//预约列表
		$openID = getOpenID();
		$data['openid'] = $openID;
		$data['debug'] = 1;
		
		$clist = webservice("Customer","BookList",$data);
		var_dump($clist);
		
		//评价详细
		
		$clist1 = webservice("Customer","consumeprojectdetail",$data);
		var_dump($clist1);
	}
}














